package org.testis.communicationmodule.services;

import org.testis.communication.client.AtofIcmpClient;
import org.testis.communication.data.CommunicationModuleDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;


/**
 * A class to define an smtp client action for
 * Communication module
 * @author nurhat
 */
public class IcmpClientAction extends AtofModuleAction
{

    /**
     * Function checks whether a given host is available
     * by using AtofSmtpClient
     * @param request
     * @return
     */
    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        CommunicationModuleDataObject dataObj =
            (CommunicationModuleDataObject) request.getDataObj();
        LogSenderService.putMessageOnQueue(
            "IcmpClientAction",
            AtofLogLevel.INFO,
            "Starting to execute icmp client action ....with data:" + dataObj.
            toString() + "....",
            dataObj.getMessageId());

        boolean result = false;

        AtofIcmpClient icmp = new AtofIcmpClient();

        result = icmp.isAvailable(dataObj.getHost(),
                                  5000);

        AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;

        LogSenderService.putMessageOnQueue(
            "IcmpClientAction",
            level,
            "IcmpClientAction executed " + (result ? "" : "un") +
            "successfully...",
            dataObj.getMessageId());
        return result;
    }
}